package com.lovestudying.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.Vector;
import java.util.Map.Entry;

import com.lovestudying.dao.AccountDao;
import com.lovestudying.dao.Impl.AccountDaoImpl;
import com.lovestudying.entity.Account;
import com.lovestudying.entity.Order;
import com.lovestudying.service.AccountService;
import com.lovestudying.util.DBUtil_Pro;
import com.lovestudying.util.PageInfo;

public class AccountServiceImpl implements AccountService {

	// 调用持久化层AccountDao-AccountDaoImpl
	AccountDao accountDao = new AccountDaoImpl();

	@Override
	public Integer AccountRegister(String account_phone, String account_name, String account_email, String account_psw,
			String account_pic, Integer account_state, Integer account_Integral) {

		// 创建一个Map集合Map<String, Object>
		Map<String, Object> value_Param = new HashMap<String, Object>();

		// 生成UUID通用唯一标识码
		String uuid = UUID.randomUUID().toString();
		// "-"替换成""空串
		String uuidReplaced = uuid.replaceAll("-", "");

		// 账户信息存储在Map集合中
		value_Param.put("account_id", uuidReplaced);
		value_Param.put("account_phone", account_phone);
		value_Param.put("account_name", account_name);
		value_Param.put("account_email", account_email);
		// 未进行密码加密
		value_Param.put("account_psw", account_psw);
		value_Param.put("account_pic", account_pic);
		value_Param.put("account_state", account_state);
		value_Param.put("account_Integral", account_Integral);

		return accountDao.InsertAccount(value_Param);

	}

	@Override
	public boolean AccountLogin(String account_phone, String account_psw) {
		// 设置一个标杆
		boolean flag = true;

		// 获取所有 List<Account>、不需要任何条件
		PageInfo<Account> pageInfo = accountDao.findAccount(null, null, null, null);
		List<Account> list = pageInfo.getData();

		// 遍历List<Account>
		for (Account account : list) {

			// 判断输入手机号是否存在
			if (account_phone.equals(account.getAccount_phone())) {

				// 手机号存在、判断密码是否正确
				if (account_psw.equals(account.getAccount_psw())) {

					// 密码正确
					flag = true;
					break;
				} else {

					// 密码不正确、重新输入密码
					flag = false;
				}
			} else {

				// 手机号不存在、账户需要注册
				flag = false;
			}
		}

		return flag;
	}

	@Override
	public PageInfo<Account> getAccount() {

		return accountDao.findAccount(null, null, null, null);
	}

	@Override
	public Account getAccountByPhone(String phone) {

		return accountDao.findAccountByPhone(phone);
	}

	@Override
	public Integer UpdateAccount(Map<String, Object> Set_Param, Map<String, Object> Where_Param) {

		return accountDao.UpdateAccount(Set_Param, Where_Param);
	}

	@Override
	public List<Account> getAccountInfo() {

		return accountDao.findAccount();
	}

	@Override
	public int deleteAccountByAccountId(String accountId) {

		return accountDao.deledeByAccountId(accountId);
	}

	@Override
	public int updateByAccountId(String accountId, Integer accountState) {

		return accountDao.updateAccountStateByAccountId(accountId, accountState);
	}

	@Override
	public List<Account> getAccountByaccountName(String accountName) {

		return accountDao.findAccountByAccountName(accountName);
	}

	@Override
	public Account getAccountById(String accountId) {
		Map<String, Object> whereParam = new HashMap<>();
		whereParam.put("account.account_id", accountId);
		if (accountDao.findAccount(whereParam, null, null, null).getData().size() == 0) {
			return null;
		} else {
			return accountDao.findAccount(whereParam, null, null, null).getData().get(0);
		}
	}

}
